package org.hank.compose_yyzs.ui.widget.dialog

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.ramcosta.composedestinations.navigation.DestinationsNavigator
import kotlinx.coroutines.launch
import org.hank.compose_yyzs.R
import org.hank.compose_yyzs.ui.theme.AppTheme
import org.hank.compose_yyzs.utils.getString
import org.hank.compose_yyzs.viewmodel.UserInfoViewModel

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/9/13 15:04
 *描述:
 */
@Composable
fun BottomUpdatePassDialog(
    state : MutableState<Boolean>,
    navigator: DestinationsNavigator,
    dismissRequest:()->Unit = {}
){
    var viewModel:UserInfoViewModel = hiltViewModel()
    var passWordOld = viewModel.passWordOld.collectAsStateWithLifecycle()
    var passWordNew = viewModel.passWordNew.collectAsStateWithLifecycle()
    var passWordConfirm = viewModel.passWordNewConfirm.collectAsStateWithLifecycle()
    var scope = rememberCoroutineScope()
    var focusManager = LocalFocusManager.current


    DialogLikeBottomSheet(showState = state, dismissRequest = dismissRequest) {
         focusManager = LocalFocusManager.current
        Column {
            Spacer(modifier = Modifier.height(16.dp))
            Box(
                modifier = Modifier
                    .padding(bottom = 16.dp)
                    .fillMaxWidth()
            ) {
                Text(
                    text = getString(R.string.userinfo_edit_pass),
                    style = MaterialTheme.typography.titleMedium.copy(),
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.align(Alignment.Center),
                    textAlign = TextAlign.Center
                )
                Image(
                    painter = painterResource(id = R.mipmap.ic_cross_x),
                    contentDescription = "",
                    modifier = Modifier
                        .align(
                            Alignment.CenterEnd
                        )
                        .padding(end = 24.dp)
                        .clickable {
                            it.invoke()
                        }
                )
            }
            Divider(color = AppTheme.colors.divider, thickness = 1.dp)
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(intrinsicSize = IntrinsicSize.Min)
                    .padding(16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = getString(resourceId = R.string.userinfo_old_pass),
                    style = MaterialTheme.typography.bodyMedium,
                    modifier = Modifier.width(80.dp)
                )
                Spacer(modifier = Modifier.width(8.dp))
                BasicTextField(
                    value = passWordOld.value,
                    singleLine = true,
                    onValueChange = {
                        scope.launch {
                            viewModel.passWordOld.emit(it)
                        }
                    },
                    modifier = Modifier
                        .weight(1f)
                        .onFocusChanged {
                            scope.launch { viewModel.passOldFocus.emit(it.hasFocus) }
                        },
                    keyboardOptions = KeyboardOptions.Default.copy(
                        keyboardType = KeyboardType.Text,
                        imeAction = ImeAction.Next
                    ),
                    keyboardActions = KeyboardActions(onNext = {
                        focusManager.moveFocus(focusDirection = FocusDirection.Next)
                    }),
                    visualTransformation = PasswordVisualTransformation(),
                    decorationBox = {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .padding(4.dp)
                        ) {
                            if (passWordOld.value.isEmpty() && !viewModel.passOldFocus.collectAsStateWithLifecycle().value) {
                                Text(
                                    text = getString(resourceId = R.string.userinfo_old_pass_hint),
                                    style = MaterialTheme.typography.bodyMedium.copy(color = AppTheme.colors.textSecondary)
                                )
                            } else
                                it.invoke()
                        }
                    }
                )
            }

            Divider(color = AppTheme.colors.divider, thickness = 1.dp)
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(intrinsicSize = IntrinsicSize.Min)
                    .padding(16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = getString(resourceId = R.string.userinfo_new_pass),
                    style = MaterialTheme.typography.bodyMedium,
                    modifier = Modifier.width(80.dp)
                )
                Spacer(modifier = Modifier.width(8.dp))
                BasicTextField(
                    value = passWordNew.value,
                    singleLine = true,
                    onValueChange = {
                        scope.launch {
                            viewModel.passWordNew.emit(it)
                        }
                    },
                    modifier = Modifier
                        .weight(1f)
                        .onFocusChanged { it ->
                            scope.launch {
                                viewModel.passNewFocus.emit(it.hasFocus)
                            }
                        },
                    keyboardOptions = KeyboardOptions.Default.copy(
                        keyboardType = KeyboardType.Text,
                        imeAction = ImeAction.Next
                    ),
                    keyboardActions = KeyboardActions(onNext = {
                        focusManager.moveFocus(focusDirection = FocusDirection.Next)
                    }),
                    visualTransformation = PasswordVisualTransformation(),
                    decorationBox = {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .padding(4.dp)
                        ) {
                            if (passWordNew.value.isEmpty() && !viewModel.passNewFocus.collectAsStateWithLifecycle().value) {
                                Text(
                                    text = getString(resourceId = R.string.userinfo_new_pass_hint),
                                    style = MaterialTheme.typography.bodyMedium.copy(color = AppTheme.colors.textSecondary),
                                )
                            } else {
                                it.invoke()
                            }
                        }
                    }
                )
            }

            Divider(color = AppTheme.colors.divider, thickness = 1.dp)
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(intrinsicSize = IntrinsicSize.Min)
                    .padding(16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = getString(resourceId = R.string.userinfo_confirm_pass),
                    style = MaterialTheme.typography.bodyMedium,
                    modifier = Modifier.width(80.dp)
                )
                Spacer(modifier = Modifier.width(8.dp))
                BasicTextField(
                    value = passWordConfirm.value,
                    singleLine = true,
                    onValueChange = {
                        scope.launch {
                            viewModel.passWordNewConfirm.emit(it)
                        }
                    },
                    modifier = Modifier
                        .weight(1f)
                        .onFocusChanged { it ->
                            scope.launch {
                                viewModel.passConfirmFocus.emit(it.hasFocus)
                            }
                        },
                    keyboardOptions = KeyboardOptions.Default.copy(
                        keyboardType = KeyboardType.Text,
                        imeAction = ImeAction.Done
                    ),
                    keyboardActions = KeyboardActions(onDone = {
                        focusManager.clearFocus()
                    }),
                    visualTransformation = PasswordVisualTransformation(),
                    decorationBox = {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .padding(4.dp)
                        ) {
                            if (passWordConfirm.value.isEmpty() && !viewModel.passConfirmFocus.collectAsStateWithLifecycle().value) {
                                Text(
                                    text = getString(resourceId = R.string.userinfo_confirm_pass_hint),
                                    style = MaterialTheme.typography.bodyMedium.copy(color = AppTheme.colors.textSecondary),
                                )
                            } else {
                                it.invoke()
                            }
                        }
                    }
                )
            }

            ActionDialogCompose({
                dismissRequest()
            }) {
                focusManager.clearFocus()
                scope.launch {
                    viewModel.updatePassWord()
                }
            }
        }
    }
}